package com.itheima;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //getParameter()方法的示例代码
        // 获取请求正文，一个名称对应一个值。
        /*String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        System.out.println(username+","+password+","+gender);*/


        //getParameterValues()方法的示例代码
        //获取请求正文，一个名称可能对应多个值
        /*String username = request.getParameter("username");
        String[] passwords = request.getParameterValues("password");
        String gender = request.getParameter("gender");
        System.out.println(username+","+ Arrays.toString(passwords)+","+gender);*/


        //getParameterNames()方法的示例代码
        //获取请求正文，一个名称一个值。但是先要获取正文名称的枚举（key的枚举）
        /*Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()){
            String name = names.nextElement();
            String value = request.getParameter(name);
            System.out.println(name+":"+value);
        }*/


        //手动封装
        //封装请求正文到User对象中
        //1.获取请求正文
        /*String username = request.getParameter("username");
        String password = request.getParameter("password");
        String gender = request.getParameter("gender");
        //2.创建一个User对象
        Student student = new Student();
        //3.把请求正文封装到user对象中
        student.setUsername(username);
        student.setPassword(password);
        student.setGender(gender);
        System.out.println(student);*/

        //使用工具类封装
        //使用apache的commons-beanutils实现封装
        /*Student student = new Student();
        try {
            BeanUtils.populate(student,request.getParameterMap());
        } catch (Exception e) {
            e.printStackTrace();
        }*/


        //解决POST方式的中文乱码
        /*request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        System.out.println(username);*/

        /*重定向特点：
 	两次请求，浏览器行为，地址栏改变，请求域中的数据会丢失
                 请求转发：
  	一次请求，服务器行为，地址栏不变，请求域中的数据不丢失*/
        /*//1.拿到请求调度对象
        RequestDispatcher rd = request.getRequestDispatcher("/RequestDemo3");
        //放入数据到请求域中
        request.setAttribute("CityCode","bj-010");
        //2.实现真正的转发操作
        rd.forward(request,response);*/


        /*//转发的目的地
        //获取请求域中的数据
        String value = (String)request.getAttribute("CityCode");
        response.getWriter().write("welcome to request demo 7    "+value);*/

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
